<?php class MAdmin extends CI_Model {
	
	function __construct()
	    {
		parent::__construct();
	    }

	function verifCompteUser($login,$mdp) {
		$sql = "SELECT * FROM user WHERE LOGIN ='$login' AND MDP ='$mdp'";
		$Q = $this->db->query($sql);		
		$id = 0;
		if ($Q->num_rows()>0){
                foreach ($Q->result_array() as $row){
                        $id = $row['ID_USER'];
                }
		}
		$Q->free_result();
		return $id;
	}
	
	function getAllClinic() {
		$sql = "SELECT * FROM clinique";			
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function getAllComment() {
		$sql = "SELECT * FROM commentaire";			
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function getAllPatient() {
		$sql = "SELECT * FROM patient";			
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function supprimerClinic($id) {
		$this->db->delete("clinique",array("id_clinique"=>$id));
	}
	
	function supprimerPatient($id) {
		$this->db->delete("patient",array("id_patient"=>$id));
	}
	
	function supprimerComment($id) {
		$this->db->delete("commentaire",array("id_commentaire"=>$id));
	}
	
	function supprimerGalImageClinic($id) {
		$this->db->delete("galerieImage",array("id_galerieImage"=>$id));
	}
	
	function supprimerPhotoSoinClinic($id) {
		$this->db->delete("photoSoin",array("id_photoSoin"=>$id));
	}
	
	function supprimerLogoClinic($id) {
		$logo = "";
		$data = array(
			'logo' => $logo,
			'id_clinique' => $id
		);
	    $this->db->where('id_clinique',$id);
	    $this->db->update('clinique', $data);
	}
	
	function validClinique($id) {
		$isOK = "1";
		$data = array(
			'isOK' => $isOK,
			'id_clinique' => $id
		);
	    $this->db->where('id_clinique',$id);
	    $this->db->update('clinique', $data);
	}
	
	function validPatient($id) {
		$isOK = "1";
		$data = array(
			'isOK' => $isOK,
			'id_patient' => $id
		);
	    $this->db->where('id_patient',$id);
	    $this->db->update('patient', $data);
	}
	
	function validComment($id) {
		$isValid = "1";
		$data = array(
			'isValid' => $isValid,
			'id_commentaire' => $id
		);
	    $this->db->where('id_commentaire',$id);
	    $this->db->update('commentaire', $data);
	}
	
	function getListClinicTri($id){
		$data = array();
		$sql = "";
		if($id == 1){
			$sql = "SELECT * FROM clinique WHERE isOK = '1' ";
		}
		else if($id == 2){
			$sql = "SELECT * FROM clinique WHERE isOK = '0' ";
		}
		else {
			$sql = "SELECT * FROM clinique ";
		}
		
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function getListPatientTri($id){
		$data = array();
		$sql = "";
		if($id == 1){
			$sql = "SELECT * FROM patient WHERE isOK = '1' ";
		}
		else if($id == 2){
			$sql = "SELECT * FROM patient WHERE isOK = '0' ";
		}
		else {
			$sql = "SELECT * FROM patient ";
		}
		
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function getListCommentTri($id){
		$data = array();
		$sql = "";
		if($id == 1){
			$sql = "SELECT * FROM commentaire WHERE isValid = '1' ";
		}
		else if($id == 2){
			$sql = "SELECT * FROM commentaire WHERE isValid = '0' ";
		}
		else {
			$sql = "SELECT * FROM commentaire ";
		}
		
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function supprimerImgPplClinic($id) {
		$image_principale = "";
		$data = array(
			'image_principale' => $image_principale,
			'id_clinique' => $id
		);
	    $this->db->where('id_clinique',$id);
	    $this->db->update('clinique', $data);
	}
		
	function supprimerDentisteClinic($id) {
		$this->db->delete("dentiste",array("id_dentiste"=>$id));
	}
	
	function supprimerImgDD($identification, $id_patient) {
		$data = array(
			'photo'.$identification => "",
			'id_patient' => $id_patient
		);
	    $this->db->where('id_patient',$id_patient);
	    $this->db->update('demandeDevis', $data);
	}
	
	function supprimerLangue($identification, $id_patient) {
		$data = array(
			'langue'.$identification => "Selectionner",
			'id_patient' => $id_patient
		);
	    $this->db->where('id_patient',$id_patient);
	    $this->db->update('demandeDevis', $data);
	}
	
	function getInfoClinicId($id_clinique) {
		$sql = "SELECT * FROM clinique WHERE id_clinique = $id_clinique";			
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function getInfoPatientId($id_patient) {
		$sql = "SELECT * FROM patient WHERE id_patient = $id_patient";	
		//echo $sql;
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function getInfoDemandeDevisPatient($id_patient) {
		$sql = "SELECT * FROM demandeDevis WHERE id_patient = $id_patient";	
		//echo $sql;
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function getDentisteToModif($id_dentiste) {
			$sql = "SELECT * FROM dentiste WHERE id_dentiste = '$id_dentiste' ";			
			$Q = $this->db->query($sql);
			if ($Q->num_rows()>0){
				foreach ($Q->result_array() as $row){
					$data[]=$row;
				}
				return $data;
			}
			$Q->free_result();
			return 0;
	}
	
	function getAllPays(){
		$sql = "SELECT * FROM pays";			
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}

	function getAllVille($id_pays){
		$sql = "SELECT * FROM ville Where id_pays = '$id_pays' ORDER BY nom_ville";
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}	
	
	function selectNomVille($id_ville) {
		$nom = "";
		$sql = "SELECT * FROM ville where id_ville = '$id_ville'";		
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$nom = $row['nom_ville'];
			}
			return $nom;
		}
		$Q->free_result();
	}
	
	function updateInfoClinique($id_clinique, $email, $mdp, $pays, $map, $ville, $adresse, $code_postal, $nom_clinique, $surface, $fauteuil, $scanner, $prothese, $marque_implant1, $marque_implant2, $marque_implant3, $origine_materiaux1, $origine_materiaux2, $origine_materiaux3){
		 $data = array(
			'email' => $email,
			'mdp' => $mdp,
			'id_pays' => $pays,
			'map' => $map,
			'id_ville' => $ville,
			'adresse' => $adresse,
			'code_postal' => $code_postal,
			'nom_clinique' => $nom_clinique,
			'surface' => $surface,
			'fauteuil' => $fauteuil,
			'scanner' => $scanner,
			'prothese' => $prothese,
			'marque_implant1' => $marque_implant1,
			'marque_implant2' => $marque_implant2,
			'marque_implant3' => $marque_implant3,
			'origine_materiaux1' => $origine_materiaux1,
			'origine_materiaux2' => $origine_materiaux2,
			'origine_materiaux3' => $origine_materiaux3,
			'id_clinique' => $id_clinique
		);
	    $this->db->where('id_clinique',$id_clinique);
	    $this->db->update('clinique', $data);
	}
	function updateInfoPatient($id_patient, $email, $mdp, $pays,  $ville, $adresse, $code_postal, $civilite, $nom, $prenom, $date_naissance, $tel_mobile, $tel_fixe){
		 $data = array(
			'email' => $email,
			'mdp' => $mdp,
			'id_pays' => $pays,
			'id_ville' => $ville,
			'adresse' => $adresse,
			'code_postal' => $code_postal,
			'civilite' => $civilite,
			'nom' => $nom,
			'prenom' => $prenom,
			'date_naissance' => $date_naissance,
			'tel_mobile' => $tel_mobile,
			'tel_fixe' => $tel_fixe,
			'id_patient' => $id_patient
		);
	    $this->db->where('id_patient',$id_patient);
	    $this->db->update('patient', $data);	
	}
	
	function updateEquipeClinique($photo_dentiste, $nom_dentiste, $fonction_dentiste, $annee_exp1, $annee_exp2, $annee_exp3, $annee_exp4, $annee_exp5, $annee_exp6, $exp1, $exp2, $exp3, $exp4, $exp5, $exp6, $langue1, $langue2, $langue3, $id_dentiste){
		$data = array(
			'photo_dentiste' => $photo_dentiste,
			'nom_dentiste' => $nom_dentiste,
			'fonction_dentiste' => $fonction_dentiste,
			'annee_exp1' => $annee_exp1,
			'exp1' => $exp1,
			'annee_exp2' => $annee_exp2,
			'exp2' => $exp2,
			'annee_exp3' => $annee_exp3,
			'exp3' => $exp3,
			'annee_exp4' => $annee_exp4,
			'exp4' => $exp4,
			'annee_exp5' => $annee_exp5,
			'exp5' => $exp5,
			'annee_exp6' => $annee_exp6,
			'exp6' => $exp6,
			'langue1' => $langue1,
			'langue2' => $langue2,
			'langue3' => $langue3,
			'id_dentiste' => $id_dentiste
		);
	    $this->db->where('id_dentiste',$id_dentiste);
	    $this->db->update('dentiste', $data);
	}
	
	function updateNomVille($ville, $nomville){
		$data = array(
			'id_ville' => $ville,
			'nom_ville' => $nomville
		);
	    $this->db->where('id_ville',$ville);
	    $this->db->update('ville', $data);
	}
	
	function updateTarifClinique($intitule_soin, $prix_soin, $id_prixSoins){
		$data = array(
			'intitule_soin' => $intitule_soin,
			'prix_soin' => $prix_soin,
			'id_prixSoins' => $id_prixSoins
		);
	    $this->db->where('id_prixSoins',$id_prixSoins);
	    $this->db->update('prixSoins', $data);	
	}
	
	function updateDemandeDevisPatient($id_patient, $budget1, $budget2, $solution_souhaite, $nb_traitement, $moment_soin, $langue1, $langue2, $langue3){
		$data = array(
			'budget1' => $budget1,
			'budget2' => $budget2,
			'solution_souhaite' => $solution_souhaite,
			'nb_traitement' => $nb_traitement,
			'moment_soin' => $moment_soin,
			'langue1' => $langue1,
			'langue2' => $langue2,
			'langue3' => $langue3,
			'id_patient' => $id_patient
		);
	    $this->db->where('id_patient',$id_patient);
	    $this->db->update('demandeDevis', $data);	
	}
	
	function getAllTarifClinique($id_clinique) {
		$sql = "SELECT * FROM prixSoins WHERE id_clinique = $id_clinique ORDER BY typePrix ASC";			
		//echo $sql;
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function getLogoImgPplClinique($id_clinique){
		$sql = "SELECT * FROM clinique Where id_clinique = '$id_clinique' ";
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function getGalerieImageClinique($id_clinique){
		$sql = "SELECT * FROM galerieImage Where id_clinique = '$id_clinique' ";
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
	function getAllDentiste($id_cliniquel) {
			$sql = "SELECT * FROM dentiste WHERE id_clinique = ?";			
			$Q = $this->db->query($sql, array($id_cliniquel));
			if ($Q->num_rows()>0){
				foreach ($Q->result_array() as $row){
					$data[]=$row;
				}
				return $data;
			}
			$Q->free_result();
			return 0;
	}
	
	function getPhotosSoinsClinique($id_clinique){
		$sql = "SELECT * FROM photoSoin Where id_clinique = '$id_clinique' ";
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
}//end class
?>